import java.time.*;
import java.util.*;
import java.util.stream.Collectors;
import java.io.*;import java.io.Serializable;

class Session implements Serializable{
    private String sessionId;
    private Movie movie;
    private ScreeningRoom room;
    private LocalDateTime time;
    private float price;
    private List<Seat> seats;
    private List<Ticket> tickets;

    public Session(String sessionId,Movie movie,ScreeningRoom room,LocalDateTime time,float price){
        this.sessionId=sessionId;
        this.movie=movie;
        this.room=room;
        this.time=time;
        this.price=price;
        this.tickets=new ArrayList<>();
    }
    private void initializeSeats(){
        seats=new ArrayList<>();
        for(int i=0;i<room.getRows();i++){
            for(int j=0;j<room.getColumns();j++){
                seats.add(new Seat(i,j));
            }
        }
    }

    public List<Seat> getAvailableSeats(){
        return seats.stream().filter(Seat::isAvailable).collect(Collectors.toList());
    }

    public boolean bookSeat(List<Seat>selectedSeats){
        for(Seat seat:selectedSeats){
            if(!seat.isAvailable()){
                return false;
            }
            seat.setAvailable(false);
        }
        return true;
    }

    public void releaseSeats(List<Seat>seatsToRelease){
        for(Seat seat:seatsToRelease){
            seat.setAvailable(true);
        }
    }

    public String getSessionId(){return sessionId;}
    public Movie getMovie(){return movie;}
    public ScreeningRoom getRoom(){return room;}
    public LocalDateTime getTime(){return time;}
    public float getPrice(){return price;}
    public List<Seat> getSeats(){return seats;}
    public List<Ticket> getTickets(){return tickets;}

    public void setMovie(Movie movie){this.movie=movie;}
    public void setTime(LocalDateTime time){this.time=time;}
    public void setPrice(float price){this.price=price;}

    @Override
    public String toString(){
        return String.format("场次ID：%s\n影片：%s\n影厅：%s\n时间：%s\n价格：%2f元\n可用座位：%d%d",sessionId,movie.getTitle(),room.getName(),time,price,getAvailableSeats().size(),room.getTotalSeats());
    }
}
